#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
// protobuf生成的头文件，包含两个信息和应用的头文件
#include "msg.grpc.pb.h"
#include "msg.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;

// 服务
class MyMsgService final : public MsgService::Service {
    Status GetMsg(ServerContext* context, const MsgRequest* request,
                  MsgResponse* reply) override {

        std::string str1("Hello");
        reply->set_sum(request->num1() + request->num2()); // 给reply.sum赋值

        return Status::OK;
    }
};

void RunServer() {
    std::string server_address("0.0.0.0:50051");
    MyMsgService service;

    ServerBuilder builder;
    // 监听
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    // 注册服务
    builder.RegisterService(&service);
    // 创建server
    std::unique_ptr<Server> server(builder.BuildAndStart());
	// 输出
    std::cout << "Server listening on " << server_address << std::endl;

    // 等待输入
    server->Wait();
}

int main(int argc, char** argv) {
  RunServer();

  return 0;
}